package com.asset.modules.system.service.impl;

import com.asset.modules.system.mapper.UserMapper;
import com.asset.modules.system.model.Company;
import com.asset.modules.system.model.Role;
import com.asset.modules.system.model.User;
import com.asset.modules.system.service.CompanyService;
import com.asset.modules.system.service.RoleService;
import com.asset.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author asset
 * @since 2020-08-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    RoleService roleService;

    @Autowired
    CompanyService companyService;

    @Override
    public User getUser(String column, String value) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value);
        User user = getOne(queryWrapper);
        return assemblyUser(user);
    }

    public User getUserById(String id){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        User user = getOne(queryWrapper);
        return assemblyUser(user);
    }

    public List<User> getAll(){
        List<User> users = list();
        List<User> result = new ArrayList<>();
        for(User u: users){

            result.add(assemblyUser(u));
        }
        return result;
    }


    @Override
    public boolean checkExistByUserName(String username) {
        User user = getUser("user_name", username);
        if(user==null)
            return false;
        return true;
    }

    @Override
    public boolean checkExistByNickName(String nickyname) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_nicky", nickyname);
        if(getOne(queryWrapper)==null)
            return false;
        return true;
    }

    @Override
    public boolean checkExistByRoleId(String id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        if(getOne(queryWrapper)==null){
            return false;
        }
        return true;
    }


    private User assemblyUser(User user){
        if(null!=user){
            Role    r = roleService.getRoleById(user.getRoleId());
            Company c = companyService.getById(user.getCompanyId());
            user.setRoleType(r.getRoleType());
            user.setRoleName(r.getRoleName());
            user.setPermission(r.getPermission());
            user.setCompanyCode(c.getCompanyCode());
            user.setCompanyName(c.getCompanyName());
        }
        return user;
    }

    @Override
    public boolean isExist(String column, String name) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, name);
        List result = list(queryWrapper);
        if(result.size()>0){
            return true;
        }
        return false;
    }
}
